import random

class pySort:
    def __init__(self):
        # 随机生成一个长度为10，范围0-100 的随机列表
        self.initList=[]
        for x in range(10):
            self.initList.append(random.randint(0,100))
    def bubbSort(self):
        # 冒泡排序
        tempList=self.initList.copy()
        for x in range(len(tempList)):
            for y in range(len(tempList)-1-x):
                if(tempList[y]>tempList[y+1]):
                    tempList[y],tempList[y+1]=tempList[y+1],tempList[y]
        return tempList
    def selectSort(self):
        # 选择排序
        tempList=self.initList.copy()
        for x in range(len(tempList)-1):
            for y in range(x+1,len(tempList)):
                if tempList[x]>tempList[y]:
                    tempList[x],tempList[y]=tempList[y],tempList[x]
        return tempList
    def insertSort(self):
        # 插入排序
        tempList=self.initList.copy()
        for x in range(1,len(tempList)):
            i=x
            while i>0:
                if tempList[i-1]>tempList[i]:
                    tempList[i],tempList[i-1]=tempList[i-1],tempList[i]
                else:
                    break
                i-=1
        return tempList

    def quickSort(self,tempList,start,end):
        # 快速排序

        if start>=end:
            return
        high,low=end,start
        mid=tempList[start]
        while high>low:
            while tempList[high]>mid and high>low:
                high-=1
            tempList[low]=tempList[high]
            while tempList[low]<=mid and high>low:
                low+=1
            tempList[high]=tempList[low]

        tempList[low]=mid
        self.quickSort(tempList,start,low-1)
        self.quickSort(tempList,low+1,end)
        return tempList
    def mergeSort(self,tempList):
        # 归并排序
        n=len(tempList)
        mid=n//2
        if n<=1:
            return tempList
        # 递归调用分解 
        leftList=self.mergeSort(tempList[0:mid])
        rightList=self.mergeSort(tempList[mid:])
        result=[]

        left_point=0
        right_point=0
        while left_point<len(leftList) and right_point<len(rightList):
            if leftList[left_point]>rightList[right_point]:
                result.append(rightList[right_point])
                right_point+=1
            else:
                result.append(leftList[left_point])
                left_point+=1
        # 加入剩下的元素
        result.extend(leftList[left_point:])
        result.extend(rightList[right_point:])

        return result



if __name__=='__main__':
    mysort=pySort()
    print(mysort.initList)
    print("冒泡排序：",mysort.bubbSort())
    print("选择排序：",mysort.selectSort())
    print("插入排序：",mysort.insertSort())
    tempList=mysort.initList.copy()

    print("快速排序：",mysort.quickSort(tempList,0,len(tempList)-1))
    tempList2=mysort.initList.copy()
    print("归并排序：",mysort.mergeSort(tempList2))